package com.tepth.shadow.ui.refresh;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhangyu.shadow.core.app.Shadow;
import com.zhangyu.shadow.core.net.RestClient;
import com.zhangyu.shadow.core.net.callback.ISuccess;
import com.tepth.shadow.ui.recycler.DataConverter;
import com.tepth.shadow.ui.recycler.MultipleRecyclerAdapter;

/**
 * 想想是不是界面通用逻辑可以这样封装
 * @author zhangyu
 * @date 2017/10/31
 */

public class RefreshHandler implements SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {
    private final SwipeRefreshLayout mRefreshLayout;
    private final PageBean mPageBean;
    private final RecyclerView mRecyclerView;
    private MultipleRecyclerAdapter mAdapter;
    private final DataConverter mDataConverter;

    private RefreshHandler(SwipeRefreshLayout refreshLayout,
                           PageBean pageBean,
                           RecyclerView recyclerView,
                           DataConverter dataConverter) {
        mRefreshLayout = refreshLayout;
        mPageBean = pageBean;
        mRecyclerView = recyclerView;
        mDataConverter = dataConverter;
        mRefreshLayout.setOnRefreshListener(this);
    }

    public static RefreshHandler create(SwipeRefreshLayout refreshLayout,
                                        RecyclerView recyclerView,
                                        DataConverter dataConverter) {
        return new RefreshHandler(refreshLayout, new PageBean(), recyclerView, dataConverter);
    }

    public void refresh() {
        mRefreshLayout.setRefreshing(true);
        Shadow.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.setRefreshing(false);
            }
        }, 2000);
    }

    public void firstPage(String url) {
        mPageBean.setDelayed(2000);

        RestClient.builder().url(url)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final JSONObject jsonObject = JSON.parseObject(response);
                        mPageBean.setTotalCount(jsonObject.getInteger("total"))
                                .setPageSize(jsonObject.getInteger("page_size"));
                        //设置adapter
                        mAdapter = MultipleRecyclerAdapter.create(mDataConverter.setJsonData(response));
                        mAdapter.setOnLoadMoreListener(RefreshHandler.this, mRecyclerView);
                        mRecyclerView.setAdapter(mAdapter);
                        mPageBean.addIndex();
                    }
                })
                .build()
                .get();
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {

    }
}
